from weatherModule import WeatherModule

""" App Constants """
MAX_TRIES = 5;
ERROR_RES = -999;
weatherModule = WeatherModule()

"""

This is the application user interface.
It runs in an endless loop, each time gives the user an option to choose from:

'1: Find out which cities will have the weather you wish for'
'2: Which cities has no rain in the next 3 days?'
'3: What is the current weather in a specific city'
'4: What will be the weather in the next 3 days in a specific city?'
'5: Today\'s average, maximum and minimum temperatures in Europe'
'0: Exit'

After the user had made his choice, it sends the user choice to the weather module 
for analysis.

"""

def read_int(msg, low, high): 
    count = 0
    
    while count<MAX_TRIES: 
        str = raw_input(msg)
        try:
            intVal = int(str)
        except ValueError:
            print 'Input is not a number, please try again'
            count += 1
            continue
        if (intVal >= low and intVal <= high) :
            return intVal
        else :
            print 'Input must be greater than %i and lower than %i' % (low, high)
            count += 1
    
    print 'Too many tries. '
    return ERROR_RES
 
   
def wish_weather():
    
    print 'Please enter your weather wish:'
    # Retrieve day
    day = read_int('The day you wish for - 1 for tomorrow, 2 for the day after tomorrow or 3 for three days ahead: ', 1, 3)    
    if (day == ERROR_RES) :
        return
    
    # Retrieve time of day
    time = read_int('The time of day you wish for - 0 for daytime , 1 for night time: ', 0, 1)
    if (time == ERROR_RES) :
        return
        
    # Retrieve min temp
    low = read_int('The minimum temperature you wish for: ', -10, 40)
    if (low == ERROR_RES) :
        return
    
    # Retrieve max temp     
    high = read_int('The maximum temperature you wish for: ', low, 50)
    if (high == ERROR_RES) :
        return
    
    weatherModule.return_by_range_day_time(low, high, day, time)        

def print_array_to_choose(array):
    
    index = 0 
    
    for item in array :
        print ('%-20s %3i' %(item, index))
        index += 1   
        
    c = read_int('Please enter your choice: ', 0, index-1)
    return c

def get_weather(current) :
    
    countries = weatherModule.get_all_countries()
    
    print 'These are the available countries:'
    ret = print_array_to_choose(countries)
    if (ret == ERROR_RES) :
        return
    
    cities = weatherModule.get_all_cities(countries[ret])
    ret = print_array_to_choose(cities)   
    if (ret == ERROR_RES) :
        return
     
    city = cities[ret]

    if current:
        weatherModule.return_current_weather_city(city)
    else :
        weatherModule.return_forecast(city)

def stats():
    weatherModule.return_stats()
    
def run_query():   
    
    print 'THIS IS WHAT WE OFFER:'
    print '----------------------'
    print '1: Find out which cities will have the weather you wish for'
    print '2: Which cities has no rain in the next 3 days?'
    print '3: What is the current weather in a specific city'
    print '4: What will be the weather in the next 3 days in a specific city?'
    print '5: Today\'s average, maximum and minimum temperatures in Europe'
    print '0: Exit'
    
    option = read_int('Please choose an option: ', 0, 5)
    if (option == ERROR_RES) :
        return;

    if option==1 :
        wish_weather()
    elif option==2 :
        weatherModule.return_no_rain()
    elif option==3 :     
        get_weather(True)
    elif option==4 :
        get_weather(False)
    elif option==5 :
        stats()
    elif option==0 :
        return False
    
    print 'The results are shown.\n\n' 
    return True


if __name__ == '__main__':
    print 'Welcome! Europe Quicky Weather is ready for you!\n'   
    more = True
    while more :
        more = run_query()

    